package com.lib.basex.widget.recycleview;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;
import java.util.List;

public class XRecycleViewProxy<T> {

    private RecyclerView mRecyclerView;
    private List<T> mListData;

    private IBaseRecycleView<T> mIBaseRecycleView;
    private RecyclerView.LayoutManager layoutManager;

    //
    private BaseRecycleAdapter<T> adapter;

    public XRecycleViewProxy(RecyclerView recyclerView) {
        this.mRecyclerView = recyclerView;
        layoutManager = new LinearLayoutManager(recyclerView.getContext());
        this.mRecyclerView.setLayoutManager(layoutManager);
        this.mListData = new ArrayList<>();
    }

    public XRecycleViewProxy<T> setDataList(List<T> dataList) {
        this.mListData = dataList;
        return this;
    }

    public XRecycleViewProxy<T> setRecycleView(IBaseRecycleView<T> mIBaseRecycleView) {
        this.mIBaseRecycleView = mIBaseRecycleView;
        return this;
    }

    public XRecycleViewProxy<T> setLayoutManager(RecyclerView.LayoutManager layoutManager) {
        this.layoutManager = layoutManager;
        return this;
    }

    public void build() {
        adapter = new BaseRecycleAdapter<>(mRecyclerView.getContext(), mListData, mIBaseRecycleView);
        this.mRecyclerView.setAdapter(adapter);
    }

    public void refresh(List<T> list, boolean isAppend) {
        if (adapter == null) {
            return;
        }

        if (null == mListData) {
            mListData = new ArrayList<>();
        }

        if (isAppend && list != null && list.size() > 0) {
            mListData.addAll(list);
        }

        if (!isAppend) {
            mListData.clear();
            if (null != list && list.size() > 0) {
                mListData.addAll(list);
            }
        }
        adapter.notifyDataSetChanged();
    }
}
